import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
//import 'package:hellow01/home/constants.dart';
import '../../Constants/constants.dart' show Constants;
import '../../Constants/constants.dart' show AppColors;
import '../../modules/me_module/me_page.dart';
import '../find_module/find_page.dart';
import '../contacts_module/contacts_page.dart';

enum ActionItems { GROUP_CHAT, ADD_FRIEND, QR_SCAN, PAYMENT, HELP }

class NavigationIconView {
  final BottomNavigationBarItem item;
  NavigationIconView(
      {Key key, String title, IconData icon, IconData activeIcon})
      : item = new BottomNavigationBarItem(
            icon:Icon(icon, size: 25),
            activeIcon: Icon(activeIcon, size: 25),
            title: new Text(title),
            backgroundColor: Colors.white);
}

class HomePage extends StatefulWidget {
  String title = '微信';
  Color headerColor;
  IndexState createState() => new IndexState();
}

class IndexState extends State<HomePage>  with SingleTickerProviderStateMixin{
  static const HeaderColor = const Color(AppColors.AppBarColor);
  TabController _pageController;
  int _currentIndex = 0;
  List<NavigationIconView> _navigationViews;
  List<Widget> _pages;
  List<Widget> _commonBarContents; //前3页的bar内容
  List<Widget> _meContents; //“我的”页面bar内容：大部分空白区域

  @override
  void dispose(){
    _pageController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    widget.headerColor = HeaderColor;
    _bulidCommonBarContents();
_bulidMeBarContents();
    _navigationViews = [
      NavigationIconView(
        title: '微信',
        icon: const IconData(0xe609, fontFamily: Constants.IconFontFamily),
        activeIcon:const IconData(0xe609, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: '通讯录',
        icon:const IconData(0xe602, fontFamily: Constants.IconFontFamily),
        activeIcon:const IconData(0xe602, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: '发现',
        icon:const IconData(0xe601, fontFamily: Constants.IconFontFamily),
        activeIcon:const IconData(0xe601, fontFamily: Constants.IconFontFamily),
      ),
      NavigationIconView(
        title: '我',
        icon:const IconData(0xe605, fontFamily: Constants.IconFontFamily),
        activeIcon:const IconData(0xe605, fontFamily: Constants.IconFontFamily),
      )
    ];
    //_pageController = PageController(initialPage: _currentIndex);
    _pageController = TabController(length: 4,vsync: this);
    
    //页列表
    _pages = [
      new Container(
          color: Colors.blue,
          //child: Image.asset('assets/images/bg_wa.jpg', fit: BoxFit.cover)),
          child: Image.asset('assets/images/ima_m.jpg', fit: BoxFit.cover)),
        ContactsPage(), //联系人
        FindClass(),    //发现
        MePage(),       //我的
    ];
  }

  ///前3个页面通用bar内容
  _bulidCommonBarContents() {
    _commonBarContents = [
      IconButton(
        icon: Icon(Icons.search, size: 28.0),
        onPressed: () {
          print('点击了按钮');
        },
      ),
      PopupMenuButton(
        itemBuilder: (BuildContext context) {
          return <PopupMenuItem<ActionItems>>[
            PopupMenuItem(
              child: _buildPopupMenuItem(Icon(Icons.chat), "发起群聊"),
              value: ActionItems.GROUP_CHAT,
            ),
            PopupMenuItem(
              child: _buildPopupMenuItem(Icon(Icons.add_call), "添加朋友"),
              value: ActionItems.ADD_FRIEND,
            ),
            PopupMenuItem(
              child: _buildPopupMenuItem(Icon(Icons.scanner), "扫一扫"),
              value: ActionItems.QR_SCAN,
            ),
            PopupMenuItem(
              child: _buildPopupMenuItem(Icon(Icons.money_off), "收付款"),
              value: ActionItems.PAYMENT,
            ),
            PopupMenuItem(
              child: _buildPopupMenuItem(Icon(Icons.help), "帮助与反馈"),
              value: ActionItems.HELP,
            )
          ];
        },
        icon: const Icon(Icons.add, size: 28.0),
        onSelected: (ActionItems selected) {
          print('点击的是$selected');
        },
      )
    ];
  }

  ///“我的”页面bar内容
  _bulidMeBarContents(){
    _meContents = [
      IconButton(
        icon: const Icon(Icons.photo_camera),onPressed: (){

        },color: Colors.black),
    ];
  }

  //右上角+号，通用弹窗内容
  _buildPopupMenuItem(Widget icon, String title) {
    return Row(
      children: <Widget>[
        icon,
        Container(width: 12.0),
        Text(
          title,
          style:
              TextStyle(color: const Color(AppColors.AppBarPopupMenuTextColor)),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    //bar颜色可切换
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
      statusBarColor: Colors.transparent,
    ));

    final BottomNavigationBar botNavBar = BottomNavigationBar(
      fixedColor: const Color(AppColors.TabIconActive),
      items: _navigationViews.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateTo(_currentIndex);
          //_pageController.animateToPage(_currentIndex,
            //  duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
        });
        print('点击的是第$index个Tab');
      },
    );

    return Scaffold(
      appBar: AppBar(
        //title: Text("微信"),
        title: Text(widget.title),
        backgroundColor: widget.headerColor,
        elevation: 0.0,
        //如果是“我的”页面，则bar内容有一个相机，并颜色白色
        actions: _currentIndex==3?_meContents:_commonBarContents,
        //backgroundColor: Colors.brown,
        //automaticallyImplyLeading: false, 隐藏返回键，或在跳转时做
      ),
      /* body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index) {
          setState(() {
            _currentIndex = index;
            switch(_currentIndex){
              case 0:
                widget.title = '微信';
                widget.headerColor = HeaderColor;break;
                case 1:
                widget.title = '通讯录';
                widget.headerColor = HeaderColor;break;
                case 2:
                widget.title = '发现';
                widget.headerColor = HeaderColor;break;
                case 3:
                widget.title = '';
                widget.headerColor = Colors.white;break;
            }
          });
        },
      ), */
      body: TabBarView(children: _pages,controller: _pageController,),
      bottomNavigationBar: botNavBar,
    );
  }
}
